package ctrl.action.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Model;
import model.beans.ClientBean;
import model.beans.ShoppingCartBean;

import util.ProjectConstants;
import util.StringUtil;
import ctrl.action.ServletAction;

public class CheckoutServletAction implements ServletAction{
	
	private ServletContext context;	
	public CheckoutServletAction(ServletContext context) {
		this.context = context;
	}
	
	@Override
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		
		String jsp = null;
		String jspRedirect = null;
		HttpSession session = request.getSession();
		Model model = (Model)context.getAttribute(ProjectConstants.PARAMMODEL);
		ClientBean user = (ClientBean)session.getAttribute(ProjectConstants.PARAMUSER);
		ShoppingCartBean cart;
		if(request.getParameter(ProjectConstants.PARAMPLACEYOURORDER) != null){
			//get the path to save PO file
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        	String poDirToday = sdf.format(new Date());
			String filePath = context.getRealPath("/" + ProjectConstants.PARAMPOFILEFOLDER + "/" + poDirToday);
			
			//check if needs to create path
        	StringUtil.createDir(filePath);
        	
			if(user != null){
				int poNumber = model.getPOCounter(user.getNumber());
				String poString = poNumber < 10? "0" + poNumber:""+ poNumber;
				String filename = ProjectConstants.PARAMPOFILEPREFIX + user.getNumber()+"_" + poString + ProjectConstants.PARAMPOFILESUFFIX ;			
				filePath +="/"+filename;
	
				cart = (ShoppingCartBean) session.getAttribute(ProjectConstants.PARAMMYCART);
				
				//Create PO
				model.createPO(filePath, user, cart, poNumber);	
				
				//Update po number for client
				model.updatePOCounter(user.getNumber());
				String urlLink = ProjectConstants.PARAMPOFILEFOLDER+ "/" + poDirToday + "/"+ filename;
				request.setAttribute(ProjectConstants.PARAMURLLINK, urlLink);
								
				// reset shopping cart after checkout
				cart = model.createShoppingCart();
				
				//remove cart and close the session
				session.removeAttribute(ProjectConstants.PARAMMYCART);					
			}
			jsp = ProjectConstants.PAGEORDERCONFIRMATION;
		}
		else if(request.getParameter(ProjectConstants.PARAMCONTINUESHOPPING) != null){
			jsp = ProjectConstants.PAGEHOME;
		}
		else{
			if( user != null){				
				jsp = ProjectConstants.PAGECHECKOUT;						
			}
			else{
				session.setAttribute(ProjectConstants.PARAMPREVIOUSPAGE, ProjectConstants.PAGECHECKOUT);			
				jspRedirect = ProjectConstants.PAGELOGIN;
			}	
		}
		
		if(jspRedirect != null){
			response.sendRedirect(context.getContextPath()+"/"+ jspRedirect);
		}
		else{
			context.getRequestDispatcher("/" + jsp + ".jspx").forward(request, response);
		}		
	}
}
